package com.mengze.linlee_iot_adnroid.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.mengze.linlee_iot_adnroid.R


class ProductAdapter(private val products: List<Product>,
                     private val onItemClick: (String) -> Unit) :
    RecyclerView.Adapter<ProductAdapter.ViewHolder>() {

    class Product(
        val imageRes: Int,
        val title: String,
        val description: String,
        val price: String,
        val originalPrice: String? = null
    )

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val image: ImageView = view.findViewById(R.id.productImage)
        val title: TextView = view.findViewById(R.id.productTitle)
        val description: TextView = view.findViewById(R.id.productDescription)
        val price: TextView = view.findViewById(R.id.productPrice)
        val originalPrice: TextView = view.findViewById(R.id.originalPrice)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.product_detail_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val product = products[position]

        with(holder) {
            image.setImageResource(product.imageRes)
            title.text = product.title
            description.text = product.description
            price.text = product.price

            // 处理原价显示逻辑
            if (product.originalPrice != null) {
                originalPrice.text = product.originalPrice
                originalPrice.visibility = View.VISIBLE
            } else {
                originalPrice.visibility = View.GONE
            }
        }
    }

    override fun getItemCount() = products.size
}
